package com.sky.controller.user;

import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.Address;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/user/addressBook")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /**
     * 添加地址列表
     * @return
     */
    @PostMapping
    @ApiOperation("添加地址列表")
    public Result addAddress(@RequestBody AddressBook addressBook) {
        log.info("添加的地址列表数据为:{}", addressBook);
        addressBookService.addAddress(addressBook);
        return Result.success();
    }

    /**
     * 查询当前用户的地址列表
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询当前用户的地址列表")
    public Result<List<AddressBook>> listUserAddress() {
        List<AddressBook> addressBooks = addressBookService.listUserAddress();
        return Result.success(addressBooks);
    }

    /**
     * 查看当前用户的默认地址
     * @return
     */
    @GetMapping("/default")
    @ApiOperation("查看默认地址")
    public Result<AddressBook> listDefaultAddress() {
        AddressBook address = addressBookService.listDefaultAddress();
        return Result.success(address);
    }

    /**
     * 设置默认地址
     * @param addressBook
     * @return
     */
    @PutMapping("/default")
    @ApiOperation("设置默认地址")
    public Result modifyDefaultAddress(@RequestBody AddressBook addressBook) {
        log.info("设置的默认地址的id为:{}", addressBook);
        addressBookService.modifyDefaultAddress(addressBook);
        return Result.success();
    }

    /**
     * 删除选中地址
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除选中地址")
    public Result throwOutAddress(@RequestParam Long id) {
        log.info("要删除的选中的地址id为:{}", id);
        addressBookService.throwOutAddress(id);
        return Result.success();
    }

    /**
     * 根据id查看地址 用于修改地址
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查看地址")
    public Result<AddressBook> listAddressById(@PathVariable Long id) {
        log.info("要查看地址的id为:{}", id);
        AddressBook addressBook = addressBookService.listAddressById(id);
        return Result.success(addressBook);
    }

    /**
     * 修改地址 需要传id
     * @param addressBook
     * @return
     */
    @PutMapping
    @ApiOperation("根据id修改地址")
    public Result modifyAddress(@RequestBody AddressBook addressBook) {
        log.info("要修改的地址的信息为:{}", addressBook);
        addressBookService.modifyAddress(addressBook);
        return Result.success();
    }
}
